/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package redman;

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author aline
 */
public class RedManTest {
    ArrayList<Socket> appskt = new ArrayList();
    ArrayList<String> appname = new ArrayList();
    public static StringBuffer toSend = new StringBuffer("");
    public void iniciarServidor(){
        TestServer ts = new TestServer(appskt,appname);
        Thread thread = new Thread(ts);
        thread.start();
    }
    private static void sendString(String s) {
        synchronized (toSend) {
            toSend.append(s + "\n");
        }
    }

    public RedManTest getAtributo() {
        return this;
    }
    public void testar(String app){
        try {
        Socket socketatual = null;
        if(appname.contains(app)){
            socketatual = appskt.get(appname.indexOf(app));
        }

        BufferedReader in = new BufferedReader(new
                InputStreamReader(socketatual.getInputStream()));
        PrintWriter out = new PrintWriter(socketatual.getOutputStream(), true);

        //Ennvio da conversao solicitada
        sendString("teste2");
if (toSend.length() != 0) {
              out.print(toSend); out.flush();
              toSend.setLength(0);
           }
        //Espera pela mensagem do cliente
        while(!in.ready()){}
        String resp = in.readLine();
        System.out.println("A resposta foi " + resp);
        if(resp.equals("4")){
            System.out.println("Resposta Correta");
        }else{
            System.out.println("Resposta Incorreta");
        }


        //Componentes do TCP sendo fechados
        out.close();
        in.close();
      }
        catch(Exception e) {
    //Mensagem caso algum erro ocorra
         System.out.print("Falha!\n");
      }
    }

    public static void main(String args[]) {
           RedManTest rmt = new RedManTest();
           rmt.iniciarServidor();
           JOptionPane.showConfirmDialog(null, "Inicializar Servidor");
           rmt.testar("app1");
       }


}
